{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Basket Rebalance Quick Start\n",
    "\n",
    "This quick start guide provide an example of rebalancing a equity custom basket via gs_quant. At the conclusion of this example, you will be able rebalance the constituents of a basket you had previously created on Marquee."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant import *\n",
    "\n",
    "GsSession.use(Environment.QA, 'CLIENT_ID', 'CLIENT_SECRET', ('read_product_data',))\n",
    "marquee_id = 'MARQUEE_ID'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define your composition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.api.gs.assets import GsAssetApi\n",
    "\n",
    "compositions = {'AAPL UW': 100, 'GS UN': 200}\n",
    "# composition_num_type can be either quantity or weight\n",
    "composition_num_type = \"quantity\"\n",
    "\n",
    "new_universe = list(compositions.keys())\n",
    "kwargs = {'listed': [True], 'assetClassificationsIsPrimary': [True]}\n",
    "positionIds = GsAssetApi.map_identifiers(\"bbid\", \"id\", new_universe, **kwargs)\n",
    "positionSet = [{'assetId': mqid, composition_num_type: compositions[bbid]} for (bbid, mqid) in positionIds.items()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set-up Basket Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.target.indices import *\n",
    "\n",
    "publishParameters = PublishParameters(False, False, False)\n",
    "pricingParameters = IndicesPriceParameters()\n",
    "pricingParameters.initialPrice = 100\n",
    "parameters = {'publishParameters': publishParameters, 'pricingParameters': pricingParameters, 'positionSet': positionSet}\n",
    "inputs = IndicesRebalanceInputs(parameters)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rebalance the Basket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.api.gs.indices import GsIndexApi\n",
    "\n",
    "index = GsIndexApi(marquee_id)\n",
    "response = index.rebalance(inputs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Cancel the Basket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cancelInputs = ApprovalAction('Cancel Comments')\n",
    "index.cancel_rebalance(cancelInputs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
